package com.attendcollatesys;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.apache.poi.ss.usermodel.*;

import java.util.ArrayList;
import java.util.List;

public class DynamicExcel {
    public static void main(String[] args) {
        // 创建 ExcelWriter 对象
        ExcelWriter excelWriter = EasyExcel.write("D:/File/work-6/1/output.xlsx").build();

        // 创建表格数据
        List<TableData> tableDataList = createTableDataList();

        // 写入表格
        for (int i = 0; i < tableDataList.size(); i++) {
            TableData tableData = tableDataList.get(i);
            WriteSheet sheet = EasyExcel.writerSheet(i, "Sheet" + (i + 1)).build();
            writeTable(excelWriter, sheet, tableData);
        }

        // 关闭 ExcelWriter 对象
        excelWriter.finish();
    }

    // 创建表格数据列表
    private static List<TableData> createTableDataList() {
        List<TableData> tableDataList = new ArrayList<>();

        // 添加表格数据，这里假设有两个月份的考勤表格
        tableDataList.add(new TableData("2022-01", 31));
        tableDataList.add(new TableData("2022-02", 28));

        return tableDataList;
    }

    // 写入一个表格
    private static void writeTable(ExcelWriter excelWriter, WriteSheet sheet, TableData tableData) {
        // 设置表头样式
        //setHeadStyle(sheet, tableData.getDays());

        // 写入表头和数据
        writeData(excelWriter, sheet, tableData);
    }


    // 写入表头和数据
    private static void writeData(ExcelWriter excelWriter, WriteSheet sheet, TableData tableData) {
        // 写入第一个表头
        List<List<String>> firstHeader = new ArrayList<>();
        List<String> firstHeaderRow = new ArrayList<>();
        firstHeaderRow.add("月度考勤"); // 第一个表头的内容为 "月度考勤"
        firstHeader.add(firstHeaderRow);
        excelWriter.write(firstHeader, sheet);

        // 写入第二个表头
        List<List<String>> secondHeader = new ArrayList<>();
        List<String> secondHeaderRow = new ArrayList<>();
        for (int i = 1; i <= tableData.getDays(); i++) {
            secondHeaderRow.add("Day " + i); // 第二个表头的内容为 "Day 1", "Day 2", ...
        }
        secondHeader.add(secondHeaderRow);
        excelWriter.write(secondHeader, sheet);

        // 写入数据
        excelWriter.write(tableData.getData(), sheet);
    }

    // 表格数据类
    static class TableData {
        private String title;
        private int days;
        private List<List<Object>> data;

        public TableData(String title, int days) {
            this.title = title;
            this.days = days;
            this.data = generateData();
        }

        public String getTitle() {
            return title;
        }

        public int getDays() {
            return days;
        }

        public List<List<Object>> getData() {
            return data;
        }

        private List<List<Object>> generateData() {
            // 在这里生成表格的实际数据，这里仅作示例，可以根据需求进行修改
            List<List<Object>> data = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                List<Object> rowData = new ArrayList<>();
                for (int j = 0; j <= days; j++) {
                    rowData.add("Data " + (i + 1) + "-" + j);
                }
                data.add(rowData);
            }
            return data;
        }
    }
}
